package myboot.myblog.controller.admin;

import myboot.myblog.constant.EnvConstant;
import myboot.myblog.constant.PageConstant;
import myboot.myblog.pojo.domain.AdminUser;
import myboot.myblog.service.*;
import myboot.myblog.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author hujh
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminUserService adminUserService;

    @PostMapping("/register")
    public void register(@RequestParam Map<String,String> params){
        adminUserService.saveAdminUser(params);
    }

    /**
     * 登陆
     */
    @PostMapping("/login")
    public String login(String userName, String password, String verifyCode, HttpSession session) {
        //1.判断验证码是否正确,从session中获取正确的session
        String trueVerifyCode = (String) session.getAttribute(EnvConstant.SUCCESS_CODE);
        if (!trueVerifyCode.equals(verifyCode)){
            session.setAttribute(EnvConstant.ERROR_MESSAGE,"验证码不正确！");
            return PageConstant.ADMIN_LOGIN;
        }
        //2.判断是否存在该用户
        AdminUser adminUser = adminUserService.findByUserName(userName);
        if (adminUser == null){
            session.setAttribute(EnvConstant.ERROR_MESSAGE,"用户名输入错误！");
            return PageConstant.ADMIN_LOGIN;
        }
        //3.判断密码是否正确
        if (!adminUser.getLoginPassword().equals(MD5Utils.MD5Encoding(password))){
            session.setAttribute(EnvConstant.ERROR_MESSAGE,"密码不正确！");
            return PageConstant.ADMIN_LOGIN;
        }
        //4.将用户存入session中
        session.setAttribute(EnvConstant.LOGIN_USER,adminUser);
        //5.返回index首页
        return "redirect:/admin/index";
    }

    /**
     * 修改登陆信息
     */
    @PostMapping("/profile/name")
    @ResponseBody
    public String profileName(String loginUserName,String nickName,HttpSession session){
        //获取当前用户
        Integer userId = (Integer) session.getAttribute("loginUserId");
        AdminUser adminUser = adminUserService.findByUserId(userId);
        AdminUser user = new AdminUser();
        user.setAdminUserId(adminUser.getAdminUserId());
        user.setLoginUsername(loginUserName);
        user.setNickName(nickName);
        if (adminUserService.updateAdminUser(user)){
            adminUser = adminUserService.findByUserId(userId);
            session.setAttribute(EnvConstant.LOGIN_USER,adminUser);
            return "success";
        }else {
            return "修改失败";
        }
    }

    /**
     * 修改密码
     */
    @PostMapping("/profile/password")
    @ResponseBody
    public String profilePassword(String originalPassword,String newPassword,HttpSession session){
        Integer userId = (Integer) session.getAttribute("loginUserId");
        AdminUser adminUser = adminUserService.findByUserId(userId);
        if (!MD5Utils.MD5Encoding(originalPassword).equals(adminUser.getLoginPassword())) {
            return "修改失败";
        }
        //修改管理员密码
        AdminUser user = new AdminUser();
        user.setAdminUserId(userId);
        user.setLoginPassword(MD5Utils.MD5Encoding(newPassword));
        if (adminUserService.updateAdminUser(user)){
            return "success";
        }else {
            return "修改失败";
        }
    }

    /**
     * 注销登陆
     */
    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.setAttribute(EnvConstant.LOGIN_USER,null);
        return "admin/login";
    }
}
